package com.koron.tool.controller;

import com.github.pagehelper.PageInfo;
import com.koron.bean.base.Response;
import com.koron.bean.system.staff.vo.EamUser;
import com.koron.bean.workflow.RedirectQuery;
import com.koron.bean.workflow.WorkflowController;
import com.koron.common.core.annocation.CasUser;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.tool.bean.ScrapListBean;
import com.koron.tool.bean.ScrapQuery;
import com.koron.tool.bean.ToolQuery;
import com.koron.tool.service.ToolScrapDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.koron.ebs.mybatis.ADOConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.net.URLEncoder;


@Slf4j
@Api(tags = "工器具报废管理")
@RestController
@RequestMapping("/toolScrap")
public class ToolScrapDataController implements WorkflowController {


    @Autowired
    private ToolScrapDataService service;

    /**
     *查询预报废管理工具
     * @param user
     * @param query
     * @return
     */
    @ApiOperation(value = "分页查询预报废管理工具")
    @GetMapping("/getYuScrapList")
    public Response<PageInfo> getYuScrapList(@CasUser @ApiIgnore EamUser user, ToolQuery query) {
        return ADOConnection.runTask(user.getCurrDs(), service, "getYuScrapList", Response.class,query);
    }

    /**
     * 移除预报废工具中手动报废的工具
     * @param user
     * @param id
     * @return
     */
    @ApiOperation(value = "移除预报废工具中手动报废的工具")
    @GetMapping("/deleteYuScrap")
    public Response deleteYuScrap(@CasUser @ApiIgnore EamUser user,String id) {
        return ADOConnection.runTask(user.getCurrDs(), service, "deleteYuScrap", Response.class, id);
    }


    /**
     * 查看报废单列表
     * @param user
     * @param query
     * @return
     */
    @ApiOperation(value = "查看报废单列表")
    @GetMapping("/getScrapList")
    public Response<PageInfo> getScrapList(@CasUser @ApiIgnore EamUser user, ScrapQuery query) {
        return ADOConnection.runTask(user.getCurrDs(), service, "getScrapList", Response.class,query);
    }

    /**
     * 生成报废单
     * @param user
     * @param query
     * @return
     */
    @ApiOperation(value = "生成报废单")
    @PostMapping("/addScrapList")
    public Response addScrapList(@CasUser @ApiIgnore EamUser user,@RequestBody ScrapListBean query,Integer operation) {
        if(query.getId()!=null){
            return ADOConnection.runTask(user.getCurrDs(), service, "updateScrapList", Response.class,user,query,operation);
        }else {
            return ADOConnection.runTask(user.getCurrDs(), service, "addScrapList", Response.class,user,query,operation);
        }

    }

    /**
     * 草稿状态修改报废单
     * @param user
     * @param query
     * @return
     */
    @ApiOperation(value = "草稿状态修改报废单")
    @PostMapping("/updateScrapList")
    public Response updateScrapList(@CasUser @ApiIgnore EamUser user,@RequestBody ScrapListBean query,Integer operation) {
        return ADOConnection.runTask(user.getCurrDs(), service, "updateScrapList", Response.class,user,query,operation);
    }

    /**
     * 流程审批回调
     * @param redirectQuery
     * @return
     */
    @ApiOperation(value = "流程审批回调")
    @Override
    public Response updateState(@RequestBody RedirectQuery redirectQuery) {
        return ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(),service, "updateStateScrap", Response.class,redirectQuery);
    }

    /**
     * 查看报废单详情
     * @param user
     * @param id
     * @return
     */
    @ApiOperation(value = "查看报废单详情")
    @GetMapping("/scrapInfo")
    public Response scrapInfo(@CasUser @ApiIgnore EamUser user,String id) {
        return ADOConnection.runTask(user.getCurrDs(), service, "scrapInfo", Response.class,id);
    }

    /**
     * 批量导入工具
     * @param file
     * @param toolTypeId
     * @return
     */
    @ApiOperation(value = "批量导入工具")
    @PostMapping("/upload")
    public Response upload(@CasUser @ApiIgnore EamUser user,MultipartFile file,String toolTypeId) {
        return ADOConnection.runTask(user.getCurrDs(),service, "upload", Response.class,user,file,toolTypeId);
    }

    @ApiOperation(value = "批量导入工具")
    @PostMapping("/uploadTest")
    public Response uploadTest(@CasUser @ApiIgnore EamUser user,MultipartFile file,String toolTypeId) {
        return ADOConnection.runTask(user.getCurrDs(),service, "uploadTest", Response.class,user,file,toolTypeId);
    }


    @SneakyThrows
    @ApiOperation(value = "报废单导出pdf")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "报废单id", required = true, dataType = "String", paramType = "query"),
    })
    @GetMapping(value = "/exportPdf")
    public void exportPdf(@CasUser @ApiIgnore EamUser user, HttpServletResponse response, String id){
        try (OutputStream os = response.getOutputStream()) {
            if (null == user) {
                return ;
            }

            response.setContentType("application/pdf");
            response.setCharacterEncoding("utf-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("检验单.pdf", "UTF-8"));
            ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), service, "exportPdf", void.class,user, id,os);
            os.flush();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }


}
